package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppGoodsCommentBo;
import com.ruoyi.app.domain.vo.AppGoodsCommentVo;
import com.ruoyi.app.domain.AppGoodsComment;
import com.ruoyi.app.mapper.AppGoodsCommentMapper;
import com.ruoyi.app.service.IAppGoodsCommentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP商品评论Service业务层处理
 *
 * @author heike
 * @date 2023-04-24
 */
@RequiredArgsConstructor
@Service
public class AppGoodsCommentServiceImpl implements IAppGoodsCommentService {

    private final AppGoodsCommentMapper baseMapper;

    /**
     * 查询APP商品评论
     */
    @Override
    public AppGoodsCommentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP商品评论列表
     */
    @Override
    public TableDataInfo<AppGoodsCommentVo> queryPageList(AppGoodsCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppGoodsComment> lqw = buildQueryWrapper(bo);
        Page<AppGoodsCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP商品评论列表
     */
    @Override
    public List<AppGoodsCommentVo> queryList(AppGoodsCommentBo bo) {
        LambdaQueryWrapper<AppGoodsComment> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(AppGoodsComment::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public AppGoodsCommentVo queryOne(Long orderId) {
        LambdaQueryWrapper<AppGoodsComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppGoodsComment::getOrderId, orderId);
        return baseMapper.selectVoOne(wrapper);
    }

    private LambdaQueryWrapper<AppGoodsComment> buildQueryWrapper(AppGoodsCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppGoodsComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AppGoodsComment::getUserId, bo.getUserId());
        lqw.eq(bo.getGoodsId() != null, AppGoodsComment::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getOrderId() != null, AppGoodsComment::getOrderId, bo.getOrderId());
        lqw.eq(bo.getStar() != null, AppGoodsComment::getStar, bo.getStar());
        lqw.eq(StringUtils.isNotBlank(bo.getDescript()), AppGoodsComment::getDescript, bo.getDescript());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), AppGoodsComment::getImages, bo.getImages());
        return lqw;
    }

    /**
     * 新增APP商品评论
     */
    @Override
    public Boolean insertByBo(AppGoodsCommentBo bo) {
        AppGoodsComment add = BeanUtil.toBean(bo, AppGoodsComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP商品评论
     */
    @Override
    public Boolean updateByBo(AppGoodsCommentBo bo) {
        AppGoodsComment update = BeanUtil.toBean(bo, AppGoodsComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppGoodsComment entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP商品评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
